﻿/**************************************************************************
创建时间:	2019/5/23
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace ZhCun.Utils
{
    public interface IApiResultExtend { }

    public static class ApiResultExtend
    {
        public static ApiResult RetOK(this IApiResultExtend e, string msg = "")
        {
            return ApiResult.RetOK(msg);
        }
        /// <summary>
        /// 成功结果，并返回实体对象的结果
        /// </summary>
        public static ApiResult<T> RetOK<T>(this IApiResultExtend e, T data, string msg = "")
        {
            return ApiResult.RetOK(data, msg);
        }
        /// <summary>
        /// 成功结果，返回DataTable 结果集，count 为分页的全部记录数
        /// </summary>
        public static ApiResultDataTable RetOK(this IApiResultExtend e, DataTable data, int count, string msg = "")
        {
            return ApiResult.RetOK(data, count, msg);
        }
        /// <summary>
        /// 返回一个实体集合结果
        /// </summary>
        public static ApiResultList<T> RetOK<T>(this IApiResultExtend e, List<T> data, int count, string msg = "")
        {
            return ApiResult.RetOK(data, count, msg);
        }
        /// <summary>
        /// 正常逻辑错误
        /// </summary>
        public static ApiResult RetErr(this IApiResultExtend e, string msg)
        {
            return ApiResult.RetErr(msg);
        }
        /// <summary>
        /// 返回数据对象的错误结果
        /// </summary>
        public static ApiResult<T> RetErr<T>(this IApiResultExtend e, string msg)
        {
            return ApiResult.RetErr<T>(msg);
        }
        /// <summary>
        /// 返回验证错误,data 表示属性名称
        /// </summary>
        public static ApiResult<string> RetErr(this IApiResultExtend e, string propertyName, string msg)
        {
            return ApiResult.RetErr(propertyName, msg);
        }
        /// <summary>
        /// 返回验证错误的list结果
        /// </summary>
        public static ApiResultList<T> RetErrList<T>(this IApiResultExtend e,string msg)
        {
            return new ApiResultList<T>
            {
                code = ApiResult.CODE_VERIFY_FAIL,
                msg = msg,
                data = null
            }; ;
        }

    }

    /// <summary>
    /// 执行结果的结果
    /// </summary>
    public class ApiResult
    {
        /// <summary>
        /// 返回 success = true
        /// </summary>
        public static bool operator true(ApiResult ret)
        {
            return ret?.success == true;
        }
        /// <summary>
        /// 返回 success 或者 null = false
        /// </summary>
        public static bool operator false(ApiResult ret)
        {
            return ret == null || ret.success == false;
        }
        /// <summary>
        /// 返回 success 或者 null = false
        /// </summary>
        public static bool operator !(ApiResult ret)
        {
            return ret?.success == false;
        }
        /// <summary>
        /// 返回是否成功bool结果
        /// </summary>
        public bool success { get { return code == CODE_SUCCESS; } }
        /// <summary>
        /// 结果代码，0为成功
        /// </summary>
        public int code { set; get; }
        /// <summary>
        /// 用户提示消息
        /// </summary>
        public string msg { set; get; } = string.Empty;
        /// <summary>
        /// 成功的返回代码
        /// </summary>
        public const int CODE_SUCCESS = 0;
        /// <summary>
        /// 当验证输入内容错误时返回此代码，用于控件获取焦点
        /// </summary>
        public const int CODE_VERIFY_FAIL = 101;
        /// <summary>
        /// 逻辑错误，仅用于提示
        /// </summary>
        public const int CODE_LOGIC_ERR = 102;
        /// <summary>
        /// http请求超时
        /// </summary>
        public const int CODE_HTTP_TIMEOUT = 401;

        public const int CODE_HTTP_FAIL = 500;

        #region 工厂

        /// <summary>
        /// 成功结果，可指定成功消息
        /// </summary>
        public static ApiResult RetOK(string msg = "")
        {
            return new ApiResult
            {
                code = CODE_SUCCESS,
                msg = msg
            };
        }
        /// <summary>
        /// 成功结果，并返回实体对象的结果
        /// </summary>
        public static ApiResult<T> RetOK<T>(T data, string msg = "")
        {
            return new ApiResult<T>
            {
                code = CODE_SUCCESS,
                msg = msg,
                data = data
            };
        }
        /// <summary>
        /// 成功结果，返回DataTable 结果集，count 为分页的全部记录数
        /// </summary>
        public static ApiResultDataTable RetOK(DataTable data, int count, string msg = "")
        {
            return new ApiResultDataTable
            {
                code = CODE_SUCCESS,
                msg = msg,
                data = data,
                count = count
            };
        }
        /// <summary>
        /// 返回一个实体集合结果
        /// </summary>
        public static ApiResultList<T> RetOK<T>(List<T> data, int count, string msg = "")
        {
            return new ApiResultList<T>
            {
                code = CODE_SUCCESS,
                msg = msg,
                data = data,
                count = count
            };
        }
        /// <summary>
        /// 正常逻辑错误
        /// </summary>
        public static ApiResult RetErr(string msg)
        {
            return new ApiResult
            {
                code = CODE_LOGIC_ERR,
                msg = msg
            };
        }
        /// <summary>
        /// 返回数据对象的错误结果
        /// </summary>
        public static ApiResult<T> RetErr<T>(string msg)
        {
            return new ApiResult<T>
            {
                code = CODE_VERIFY_FAIL,
                msg = msg,
                data = default
            };
        }

        /// <summary>
        /// 返回验证错误,data 表示属性名称
        /// </summary>
        public static ApiResult<string> RetErr(string propertyName, string msg)
        {
            return new ApiResult<string>
            {
                code = CODE_VERIFY_FAIL,
                msg = msg,
                data = propertyName
            };
        }

        /// <summary>
        /// 返回一个空的实体集合结果
        /// </summary>
        public static ApiResultList<T> RetErrList<T>(string msg = "")
        {
            return new ApiResultList<T>
            {
                code = CODE_VERIFY_FAIL,
                msg = msg,
            };
        }

        #endregion

    }
    /// <summary>
    /// 包含一个指定对象的执行结果
    /// </summary>
    public class ApiResult<T> : ApiResult
    {
        /// <summary>
        /// api返回的数据
        /// </summary>
        public T data { set; get; }
    }
    /// <summary>
    /// 返回一个数据源的数据结果(如：DataTable,DataSet 等)
    /// </summary>
    public class ApiResultData<T> : ApiResult
    {
        /// <summary>
        /// 数据源
        /// </summary>
        public T data { set; get; }
        /// <summary>
        /// 如果分页返回总行数
        /// </summary>
        public int count { set; get; }
    }
    /// <summary>
    /// 返回一个 DataTable 结果集带行数的结果
    /// </summary>
    public class ApiResultDataTable : ApiResultData<DataTable>
    { }
    /// <summary>
    /// 返回一个包含集合数据结果
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ApiResultList<T> : ApiResult
    {
        /// <summary>
        /// 数据源
        /// </summary>
        public List<T> data { set; get; }
        /// <summary>
        /// 如果分页返回总行数
        /// </summary>
        public int count { set; get; }
        /// <summary>
        /// 返回总页数
        /// </summary>
        public int pageCount { set; get; }
    }
}